package com.anda.logistics.action;

import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;

import com.anda.logistics.model.SystemCode;
import com.anda.logistics.model.TruckInfo;
import com.anda.logistics.service.DriverLocationService;
import com.anda.logistics.service.SystemCodeService;
import com.anda.logistics.util.JQGridJsonData;
import com.anda.logistics.util.JQGridPager;
import com.anda.logistics.vo.LocationStatVo;

@ParentPackage("json-default")
public class DriverLocationAction extends AndaBaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 4751660345511961373L;
	private DriverLocationService driverLocationService;
	private List<LocationStatVo> list;
	private List<TruckInfo> listDetail;
	private int level = 1;
	private String plateNo;
	private String region;
	private String city;
	private SystemCodeService systemCodeService;
	private Map<String,List<SystemCode>> mapCode;
	private String a;
	private JQGridPager pager;
	private JQGridJsonData results;
	
	@Action(value="/manager/location/stat"
			,results=@Result(location="stat.jsp",name="success"))
	public String query(){
		//mapCode = this.systemCodeService.getAllPartedCode();
		list = driverLocationService.getStat(level, plateNo, region);
//		for(LocationStat stat: list){
//			//System.out.println(stat);
//		}
		return SUCCESS;
	}
	@Action(value="/manager/location/statDetail"
			,results=@Result(location="stat-detail.jsp",name="success"))
	public String queryDetail(){
		//listDetail = driverLocationService.getStat(plateNo, region, city);
		mapCode = this.systemCodeService.getAllPartedCode();
		return SUCCESS;
	}
	
	@Action(value="/manager/location/stat-detail-list"
		,results=@Result(name="success",type="json",params={"root","results","contentType","text/html","enableGZIP","true","noCache","true"})
	)
	public String statDetailList(){
		mapCode = this.systemCodeService.getAllPartedCode();
		try {
			city = java.net.URLDecoder.decode(city,"UTF-8");
			region = java.net.URLDecoder.decode(region,"UTF-8");
			plateNo = java.net.URLDecoder.decode(plateNo,"UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		List<TruckInfo> list = driverLocationService.getPagerTruckList(pager,city,region,plateNo);
		long records = driverLocationService.getPagerTruckRecords(pager,city,region,plateNo);
		long total = records / pager.getRows();
		if ( records % pager.getRows() > 0) total ++;
		results = new JQGridJsonData();
		results.setPage(pager.getPage());
		results.setRecords(records);
		results.setTotal(total);
		results.setRowsFromList(list,"id"
			,new String[]{"id","locProvince","locCity","locRegion","driverName","driverSex","truckState","truckBrand"
				,"truckType","bodyStruc","capacity","truckLength","truckVolumn","plateNo","runningToken","drivingLicense"
				,"licenseType","mobileNo","freqLine","makeTime","modifyTime"
				,"lat","lng","getTimeFormatSecond","cityCode","addressDetail","locationDesc"
				});
		return SUCCESS;
	}
	
	@Resource
	public void setDriverLocationService(DriverLocationService driverLocationService){
		this.driverLocationService = driverLocationService;
	}
	
	public List<LocationStatVo> getList(){
		return list;
	}

	public int getLevel() {
		return level;
	}

	public void setLevel(int level) {
		this.level = level;
	}

	public String getPlateNo() {
		return plateNo;
	}

	public void setPlateNo(String plateNo) {
		this.plateNo = plateNo;
	}

	public String getRegion() {
		return region;
	}

	public void setRegion(String region) {
		this.region = region;
	}
	public String getCity() {
		return city;
	}
	public void setCity(String city) {
		this.city = city;
	}
	
	public SystemCodeService getSystemCodeService() {
		return systemCodeService;
	}
	@Resource
	public void setSystemCodeService(SystemCodeService systemCodeService) {
		this.systemCodeService = systemCodeService;
	}
	public Map<String, List<SystemCode>> getMapCode() {
		return mapCode;
	}
	public void setMapCode(Map<String, List<SystemCode>> mapCode) {
		this.mapCode = mapCode;
	}
	
	public String getMapTruckType(){
		return systemCodeService.getMapCode(mapCode,"trucktype");
	}
	public String getMapRegion(){
		return systemCodeService.getMapCode(mapCode,"regioncode");
	}
	public String getMapBodyStruc(){
		return systemCodeService.getMapCode(mapCode,"bodystruc");
	}
	public String getMapSex(){
		return systemCodeService.getMapCode(mapCode,"sex");
	}
	public String getMapLicenseType(){
		return systemCodeService.getMapCode(mapCode,"licensetype");
	}
	public String getMapCapacity(){
		return systemCodeService.getMapCode(mapCode,"capacity");
	}
	public String getMapTruckLength(){
		return systemCodeService.getMapCode(mapCode,"trucklength");
	}
	public String getMapTruckState(){
		return systemCodeService.getMapCode(mapCode, "truckstate");
	}
	public String getA() {
		return a;
	}
	public void setA(String a) {
		this.a = a;
	}
	public JQGridPager getPager() {
		return pager;
	}
	public void setPager(JQGridPager pager) {
		this.pager = pager;
	}
	public JQGridJsonData getResults() {
		return results;
	}
	public void setResults(JQGridJsonData results) {
		this.results = results;
	}
	
	
}
